package dingding;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceListRequest;
import com.dingtalk.api.response.OapiAttendanceListResponse;
import com.taobao.api.ApiException;

import dingding.entity.RecordResultEntity;

import dingding.entity.TopEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AttendanceDemo {

    /**
     * 获取考勤打卡记录
     *
     * @param args
     * @throws Exception 
     */
    public static void main(String args[]) throws Exception {
        // 获取服务端接口调用凭证access_token
        String access_token = AccessTokenUtil.getToken();
        UserList usl = new UserList();
        List<String> uslsList = usl.getUserId();
        ArrayList<String> arrayList=new ArrayList<String>(); //打卡结果
        ArrayList<RecordResultEntity>  arrayList2 =new ArrayList<RecordResultEntity>(); //打卡结果集合
        try {
            // 通过调用接口获取考勤打卡结果
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
            
            for (String uslm : uslsList) {
            	  OapiAttendanceListRequest request = new OapiAttendanceListRequest();
                  // 查询考勤打卡记录的起始工作日
            	  String startTime = getPastDate(7);
                  request.setWorkDateFrom(startTime);
                  //系统当前日期时间
                  SimpleDateFormat endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  // 查询考勤打卡记录的结束工作日
                  request.setWorkDateTo(endTime.format(new Date()));
                  // 员工在企业内的userid列表，最多不能超过50个。
                  request.setUserIdList(Arrays.asList(uslm));
                  // 表示获取考勤数据的起始点
                  request.setOffset(0L);
                  // 表示获取考勤数据的条数，最大不能超过50条。
                  request.setLimit(1L);
                  OapiAttendanceListResponse response = client.execute(request, access_token);
                  System.out.println("body:" +response.getBody());
     
			}
            
          
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
    
    
    
    /** 
     * 获取过去第几天的日期 
     * 
     * @param past 
     * @return 
     */  
    public static String getPastDate(int past) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
        Date today = calendar.getTime();  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String result = format.format(today);   
        return result;  
    }  
    
}




